package com.java2404.micro.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.java2404.micro.common.dto.GoodsDTO;
import com.java2404.micro.common.entity.Goods;
import com.java2404.micro.common.entity.Merchant;
import com.java2404.micro.common.entity.PageBean;
import com.java2404.micro.merchant.mapper.GoodsMapper;
import com.java2404.micro.merchant.mapper.MerchantMapper;
import com.java2404.micro.merchant.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Resource
    private GoodsMapper goodsMapper;
    @Resource
    private MerchantMapper merchantMapper;

    @Override
    public void AddGoods(Goods goods) {
        goodsMapper.insert(goods);
    }

    @Override
    public void unsale(Integer gid) {
        LambdaUpdateWrapper<Goods> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Goods::getGid,gid)
                .set(Goods::getGstatus,4);
        boolean result = this.update(updateWrapper);
    }

    @Override
    public PageBean<GoodsDTO> ShowGoods(Integer pageNum, Integer pageSize) {

            PageBean<GoodsDTO> pb = new PageBean<>();
            // 分页查询
            PageHelper.startPage(pageNum, pageSize);

            // 查询所有商品
            QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
            List<Goods> goodsList = goodsMapper.selectList(queryWrapper);

            // 构建返回的Page对象
            Page<Goods> p = (Page<Goods>) goodsList;

            // 填充PageBean的分页信息
            pb.setTotal(p.getTotal());

            // 创建商品DTO列表
            List<GoodsDTO> goodsDTOList = new ArrayList<>();

            // 遍历商品列表，填充DTO信息
            for (Goods goods : goodsList) {
                GoodsDTO goodsDTO = new GoodsDTO();
                // 填充商品信息
                goodsDTO.setGname(goods.getGname());
                goodsDTO.setMid(goods.getMid());
                goodsDTO.setGid(goods.getGid());
                goodsDTO.setGtype(goods.getGtype());
                goodsDTO.setGprice(goods.getGprice());
                goodsDTO.setGstock(goods.getGstock());
                goodsDTO.setGused(goods.getGused());
                goodsDTO.setGsurplus(goods.getGsurplus());
                goodsDTO.setGstatus(goods.getGstatus());
                goodsDTO.setCreate_time(goods.getCreate_time());

                // 根据商品的商家ID查询商家信息
                QueryWrapper<Merchant> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("mid", goods.getMid());  // 根据商户ID进行查询
                Merchant merchant = merchantMapper.selectOne(queryWrapper2);
                System.out.println(merchant);
                goodsDTO.setMerchant(merchant);  // 设置商家信息

                // 将DTO添加到列表
                goodsDTOList.add(goodsDTO);
            }

            pb.setItems(goodsDTOList);

        return pb;
    }

    @Override
    public GoodsDTO getGoodsById(Integer gid) {
        Goods goods = goodsMapper.selectById(gid);

        //如果商品的商家ID存在，则根据商家ID查询商家信息
        Merchant merchant = merchantMapper.selectById(goods.getMid());
        GoodsDTO goodsDTO;
        goodsDTO = new GoodsDTO(merchant,
                goods.getMid(),
                goods.getGid(),
                goods.getGname(),
                goods.getGtype(),
                goods.getGprice(),
                goods.getGstock(),
                goods.getGused(),
                goods.getGsurplus(),
                goods.getGstatus(),
                goods.getCreate_time(),
                goods.getUpdate_time());
        return goodsDTO;
    }

    @Override
    public void sale(Integer gid) {
        LambdaUpdateWrapper<Goods> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Goods::getGid,gid)
                .set(Goods::getGstatus,3);
        boolean result = this.update(updateWrapper);
    }
}
